home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / CodeWarrior Lite / Metrowerks C⁄C++ Lite / Headers / STL Headers / vector.h < prev   
Encoding:
C/C++ Source or Header  |  1995-04-19  |  8.7 KB  |  285 lines  |  [TEXT/MMCC]

  1. /*
  2.  *
  3.  * Copyright (c) 1994
  4.  * Hewlett-Packard Company
  5.  *
  6.  * Permission to use, copy, modify, distribute and sell this software
  7.  * and its documentation for any purpose is hereby granted without fee,
  8.  * provided that the above copyright notice appear in all copies and
  9.  * that both that copyright notice and this permission notice appear
  10.  * in supporting documentation.  Hewlett-Packard Company makes no
  11.  * representations about the suitability of this software for any
  12.  * purpose.  It is provided "as is" without express or implied warranty.
  13.  *
  14.  */
  15.  
  16. #ifndef VECTOR_H
  17. #define VECTOR_H
  18.  
  19. #include <function.h>
  20. #include <algobase.h>
  21. #include <bool.h>
  22.  
  23. #ifndef Allocator
  24. #define Allocator allocator
  25. #include <defalloc.h>
  26. #endif
  27.  
  28. #ifndef vector
  29. #define vector vector
  30. #endif
  31.  
  32. template <class T>
  33. class vector {
  34. public:
  35.     
  36.     typedef Allocator<T> vector_allocator;
  37.     typedef T value_type;
  38.     typedef vector_allocator::pointer pointer;
  39.     typedef vector_allocator::pointer iterator;
  40.     typedef vector_allocator::const_pointer const_iterator;
  41.     typedef vector_allocator::reference reference;
  42.     typedef vector_allocator::const_reference const_reference;
  43.     typedef vector_allocator::size_type size_type;
  44.     typedef vector_allocator::difference_type difference_type;
  45.     typedef reverse_iterator<const_iterator, value_type, const_reference, 
  46.                              difference_type>  const_reverse_iterator;
  47.     typedef reverse_iterator<iterator, value_type, reference, difference_type>
  48.         reverse_iterator;
  49. protected:
  50.     static Allocator<T> static_allocator;
  51.     iterator start;
  52.     iterator finish;
  53.     iterator end_of_storage;
  54.     void insert_aux(iterator position, const T& x);
  55. public:
  56.     iterator begin() { return start; }
  57.     const_iterator begin() const { return start; }
  58.     iterator end() { return finish; }
  59.     const_iterator end() const { return finish; }
  60.     reverse_iterator rbegin() { return reverse_iterator(end()); }
  61.     const_reverse_iterator rbegin() const { 
  62.         return const_reverse_iterator(end()); 
  63.     }
  64.     reverse_iterator rend() { return reverse_iterator(begin()); }
  65.     const_reverse_iterator rend() const { 
  66.         return const_reverse_iterator(begin()); 
  67.     }
  68.     size_type size() const { return size_type(end() - begin()); }
  69.     size_type max_size() const { return static_allocator.max_size(); }
  70.     size_type capacity() const { return size_type(end_of_storage - begin()); }
  71.     bool empty() const { return begin() == end(); }
  72.     reference operator[](size_type n) { return *(begin() + n); }
  73.     const_reference operator[](size_type n) const { return *(begin() + n); }
  74.     vector() : start(0), finish(0), end_of_storage(0) {}
  75.     vector(size_type n, const T& value = T()) {
  76.     start = static_allocator.allocate(n);
  77.     uninitialized_fill_n(start, n, value);
  78.     finish = start + n;
  79.     end_of_storage = finish;
  80.     }
  81.     vector(const vector<T>& x) {
  82.     start = static_allocator.allocate(x.end() - x.begin());
  83.     finish = uninitialized_copy(x.begin(), x.end(), start);
  84.     end_of_storage = finish;
  85.     }
  86.     vector(const_iterator first, const_iterator last) {
  87.     size_type n = 0;
  88.     distance(first, last, n);
  89.     start = static_allocator.allocate(n);
  90.     finish = uninitialized_copy(first, last, start);
  91.     end_of_storage = finish;
  92.     }
  93.     ~vector() { 
  94.     destroy(start, finish);
  95.     static_allocator.deallocate(start);
  96.     }
  97.     vector<T>& operator=(const vector<T>& x);
  98.     void reserve(size_type n) {
  99.     if (capacity() < n) {
  100.         iterator tmp = static_allocator.allocate(n);
  101.         uninitialized_copy(begin(), end(), tmp);
  102.         destroy(start, finish);
  103.         static_allocator.deallocate(start);
  104.         finish = tmp + size();
  105.         start = tmp;
  106.         end_of_storage = begin() + n;
  107.     }
  108.     }
  109.     reference front() { return *begin(); }
  110.     const_reference front() const { return *begin(); }
  111.     reference back() { return *(end() - 1); }
  112.     const_reference back() const { return *(end() - 1); }
  113.     void push_back(const T& x) {
  114.     if (finish != end_of_storage) {
  115.         /* Borland bug */
  116.         construct(finish, x);
  117.         finish++;
  118.     } else
  119.         insert_aux(end(), x);
  120.     }
  121.     void swap(vector<T>& x) {
  122.     ::swap(start, x.start);
  123.     ::swap(finish, x.finish);
  124.     ::swap(end_of_storage, x.end_of_storage);
  125.     }
  126.     iterator insert(iterator position, const T& x) {
  127.     size_type n = position - begin();
  128.     if (finish != end_of_storage && position == end()) {
  129.         /* Borland bug */
  130.         construct(finish, x);
  131.         finish++;
  132.     } else
  133.         insert_aux(position, x);
  134.     return begin() + n;
  135.     }
  136.     void insert (iterator position, const_iterator first, 
  137.          const_iterator last);
  138.     void insert (iterator position, size_type n, const T& x);
  139.     void pop_back() {
  140.     /* Borland bug */
  141.         --finish;
  142.         destroy(finish);
  143.     }
  144.     void erase(iterator position) {
  145.     if (position + 1 != end())
  146.         copy(position + 1, end(), position);
  147.     /* Borland bug */
  148.     --finish;
  149.     destroy(finish);
  150.     }
  151.     void erase(iterator first, iterator last) {
  152.     vector<T>::iterator i = copy(last, end(), first);
  153.     destroy(i, finish);
  154.     // work around for destroy(copy(last, end(), first), finish);
  155.     finish = finish - (last - first); 
  156.     }
  157. };
  158.  
  159. template <class T>
  160. inline bool operator==(const vector<T>& x, const vector<T>& y) {
  161.     return x.size() == y.size() && equal(x.begin(), x.end(), y.begin());
  162. }
  163.  
  164. template <class T>
  165. inline bool operator<(const vector<T>& x, const vector<T>& y) {
  166.     return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
  167. }
  168.  
  169.  
  170. template <class T>
  171. vector<T>::vector_allocator vector<T>::static_allocator;
  172.  
  173.  
  174. template <class T>
  175. vector<T>& vector<T>::operator=(const vector<T>& x) {
  176.     if (&x == this) return *this;
  177.     if (x.size() > capacity()) {
  178.     destroy(start, finish);
  179.     static_allocator.deallocate(start);
  180.     start = static_allocator.allocate(x.end() - x.begin());
  181.     end_of_storage = uninitialized_copy(x.begin(), x.end(), start);
  182.     } else if (size() >= x.size()) {
  183.     vector<T>::iterator i = copy(x.begin(), x.end(), begin());
  184.     destroy(i, finish);
  185.     // work around for destroy(copy(x.begin(), x.end(), begin()), finish);
  186.     } else {
  187.     copy(x.begin(), x.begin() + size(), begin());
  188.     uninitialized_copy(x.begin() + size(), x.end(), begin() + size());
  189.     }
  190.     finish = begin() + x.size();
  191.     return *this;
  192. }
  193.  
  194. template <class T>
  195. void vector<T>::insert_aux(iterator position, const T& x) {
  196.     if (finish != end_of_storage) {
  197.     construct(finish, *(finish - 1));
  198.     copy_backward(position, finish - 1, finish);
  199.     *position = x;
  200.     ++finish;
  201.     } else {
  202.     size_type len = size() ? 2 * size() 
  203.         : static_allocator.init_page_size();
  204.     iterator tmp = static_allocator.allocate(len);
  205.     uninitialized_copy(begin(), position, tmp);
  206.     construct(tmp + (position - begin()), x);
  207.     uninitialized_copy(position, end(), tmp + (position - begin()) + 1); 
  208.     destroy(begin(), end());
  209.     static_allocator.deallocate(begin());
  210.     end_of_storage = tmp + len;
  211.     finish = tmp + size() + 1;
  212.     start = tmp;
  213.     }
  214. }
  215.  
  216. template <class T>
  217. void vector<T>::insert(iterator position, size_type n, const T& x) {
  218.     if (n == 0) return;
  219.     if (end_of_storage - finish >= n) {
  220.     if (end() - position > n) {
  221.         uninitialized_copy(end() - n, end(), end());
  222.         copy_backward(position, end() - n, end());
  223.         fill(position, position + n, x);
  224.     } else {
  225.         uninitialized_copy(position, end(), position + n);
  226.         fill(position, end(), x);
  227.         uninitialized_fill_n(end(), n - (end() - position), x);
  228.     }
  229.     finish += n;
  230.     } else {
  231.     size_type len = size() + max(size(), n);
  232.     iterator tmp = static_allocator.allocate(len);
  233.     uninitialized_copy(begin(), position, tmp);
  234.     uninitialized_fill_n(tmp + (position - begin()), n, x);
  235.     uninitialized_copy(position, end(), tmp + (position - begin() + n));
  236.     destroy(begin(), end());
  237.     static_allocator.deallocate(begin());
  238.     end_of_storage = tmp + len;
  239.     finish = tmp + size() + n;
  240.     start = tmp;
  241.     }
  242. }
  243.  
  244. template <class T>
  245. void vector<T>::insert(iterator position, 
  246.                const_iterator first, 
  247.                const_iterator last) {
  248.     if (first == last) return;
  249.     size_type n = 0;
  250.     distance(first, last, n);
  251.     if (end_of_storage - finish >= n) {
  252.     if (end() - position > n) {
  253.         uninitialized_copy(end() - n, end(), end());
  254.         copy_backward(position, end() - n, end());
  255.         copy(first, last, position);
  256.     } else {
  257.         uninitialized_copy(position, end(), position + n);
  258.         copy(first, first + (end() - position), position);
  259.         uninitialized_copy(first + (end() - position), last, end());
  260.     }
  261.     finish += n;
  262.     } else {
  263.     size_type len = size() + max(size(), n);
  264.     iterator tmp = static_allocator.allocate(len);
  265.     uninitialized_copy(begin(), position, tmp);
  266.     uninitialized_copy(first, last, tmp + (position - begin()));
  267.     uninitialized_copy(position, end(), tmp + (position - begin() + n));
  268.     destroy(begin(), end());
  269.     static_allocator.deallocate(begin());
  270.     end_of_storage = tmp + len;
  271.     finish = tmp + size() + n;
  272.     start = tmp;
  273.     }
  274. }
  275.  
  276. #undef Allocator
  277. #undef vector
  278.  
  279. #endif
  280.  
  281.  
  282.  
  283.  
  284.  
  285.